import DTO.LocaldataDTO;
import Entity.Localdata;
//weishenme不用加链接数据库代码xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

/**
 * The sevelet class to insert Person into database
 */
@SuppressWarnings("Duplicates")
@WebServlet( urlPatterns = {"/Insertcord1"})
public class Insertcord1Servlet extends HttpServlet {

    private static final EntityManagerFactory entityManagerFactory;
    private static final String PERSISTENCE_UNIT_NAME = "MyJPADemoPU";

    static {
        entityManagerFactory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
    }

    private EntityManager getEntityManager() {
        return entityManagerFactory.createEntityManager();
    }

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request  servlet request
     * @param response servlet response
     */
    private void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        LocaldataDTO localdataDTO;
        EntityManager em = getEntityManager();

        EntityTransaction tx = em.getTransaction();

        try {

            //Get the data from user's form
//            String id = request.getParameter("id");
            String userid = request.getParameter("userid");
            String password = request.getParameter("password");
            String localname = request.getParameter("localname");
            String phone = request.getParameter("phone");
            String dgutid = request.getParameter("dgutid");
            //Create a person instance out of it
            Localdata localdata=new Localdata(userid,password,localname,phone,dgutid,"picture/no.jpg");
            localdataDTO=new LocaldataDTO(userid,password,localname,phone,dgutid,"picture/no.jpg");
            request.getSession().setAttribute("localdataDTO",localdataDTO);
            tx.begin();
            em.persist(localdata);
            tx.commit();
        } catch (Exception ex) {
            throw new ServletException(ex);
        } finally {
            em.close();
        }
        response.setContentType("text/html");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().println("本地注册成功");
        response.sendRedirect("/HomePage.jsp");
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request  servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request  servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>

}

